home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Technology Seed / Jan. '98 ATS.toast / NavServices1.0b3 / Navigation Services SDK / Examples / Sampler / Sampler ƒ / event.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-01-12  |  13.8 KB  |  538 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        event.c
  3.  
  4.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  5.  
  6. */
  7.  
  8. #pragma segment AppSeg
  9.  
  10. #ifndef Common_Defs
  11. #include "Common.h"
  12. #endif
  13.  
  14. extern Document*    gFrontDocument;
  15. extern short         gQuit, gQuitting;
  16. extern short         gInBackground;
  17. extern Boolean        gCanDrag;
  18.  
  19. void DoContent(Document* theDocument, EventRecord* theEvent);
  20. void DoBackgroundContent(Document* theDocument, EventRecord* theEvent);
  21. void DoMouseDown(EventRecord* theEvent);
  22. void DoKey(char theChar);
  23. void DoKeyDown(EventRecord* theEvent);
  24. void DoActivate(EventRecord* theEvent);
  25. void DoUpdate(EventRecord* theEvent);
  26. void DoOSEvent(EventRecord* theEvent);
  27. void DoHighLevelEvent(EventRecord* theEvent);
  28. void DoEvent(EventRecord* theEvent);
  29. void DoIdle(void);
  30.  
  31. extern ControlActionUPP myActionProcUPP;    // for Mixed Mode
  32.  
  33. #define    SleepDuration        20        // WaitNextEvent() sleep constant
  34.  
  35. // *****************************************************************************
  36. // *
  37. // *    DoContent()
  38. // *
  39. // *    Handles mouseDown events in the content region of a document window.
  40. // *
  41. // *    (1)    If the mouseDown is on a control, handle the click by calling TrackControl.
  42. // *
  43. // *    (2)    If the mouseDown is on a draggable object (the document's hiliteRgn) and a
  44. // *        successful drag occurs, no further processing is necessary.
  45. // *
  46. // *    (3)    If the mouseDown is on a draggable object and the mouse is released without
  47. // *        dragging, set the insertion point to the original mouseDown location by calling
  48. // *        TEClick with the mouseDown information.
  49. // *
  50. // *    (4)    If the mouseDown is not on a draggable object and within the viewRect of the
  51. // *        TextEdit field, call TEClick to handle the mouseDown.
  52. // *
  53. // *****************************************************************************
  54. void DoContent(Document* theDocument, EventRecord* theEvent)
  55. {    
  56.     short            thePart;
  57.     Point            thePoint;
  58.     ControlHandle    theControl;
  59.  
  60.     if (theDocument->theTE != NULL)
  61.         {
  62.         SetPort(theDocument->theWindow);
  63.  
  64.         thePoint = theEvent->where;
  65.         GlobalToLocal(&thePoint);
  66.  
  67.         if (thePart = FindControl(thePoint,(WindowRef)theDocument->theWindow,&theControl))
  68.             {
  69.             if (theControl == theDocument->vScroll)
  70.                 {
  71.                 if (thePart == kControlIndicatorPart)
  72.                     {
  73.                     TrackControl(theControl,thePoint,0L);
  74.                     AdjustDocumentView(theDocument);
  75.                     }
  76.                 else
  77.                     TrackControl(theControl,thePoint,myActionProcUPP);
  78.  
  79.                 AdjustScrollBar(theDocument);
  80.                 }
  81.             }
  82.         else
  83.             if (PtInRgn(thePoint,theDocument->hiliteRgn))
  84.                 {
  85.                 if (gCanDrag)
  86.                     if (!DragText(theDocument,theEvent,theDocument->hiliteRgn))
  87.                         TEClick(thePoint,false,theDocument->theTE);
  88.                 }
  89.             else
  90.                 if (PtInRect(thePoint,&(**(theDocument->theTE)).viewRect))
  91.                     TEClick(thePoint,(theEvent->modifiers & shiftKey) != 0,theDocument->theTE);
  92.  
  93.         if (gCanDrag)
  94.             TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
  95.         }
  96. }
  97.  
  98.  
  99. // *****************************************************************************
  100. // *
  101. // *    DoBackgroundContent()
  102. // *
  103. // *    Handles mouseDown events in the content region of a document window
  104. // *    when the window is not frontmost. The following bullet items describe how this background
  105. // *    mouseDown event is handled:
  106. // *
  107. // *    (1)    If the mouseDown is not in a draggable object (not in the document's hiliteRgn) call
  108. // *        SelectWindow to bring the window to the front as usual.
  109. // *
  110. // *    (2)    If the mouseDown is in a draggable object and the mouse is released without
  111. // *        dragging, call SelectWindow when the mouse is released.
  112. // *
  113. // *    (3)    If the mouseDown is in a draggable object and a successful drag occurs, SelectWindow
  114. // *        should only be called if the drop occurred in the same window (the DragText function
  115. // *        calls SelectWindow in this case).
  116. // *
  117. // *****************************************************************************
  118. void DoBackgroundContent(Document* theDocument, EventRecord* theEvent)
  119. {    
  120.     Point    thePoint;
  121.  
  122.     SetPort((GrafPtr)theDocument->theWindow);
  123.  
  124.     thePoint = theEvent->where;
  125.     GlobalToLocal(&thePoint);
  126.  
  127.     if (PtInRgn(thePoint,theDocument->hiliteRgn))
  128.         {
  129.         if (!DragText(theDocument,theEvent,theDocument->hiliteRgn))
  130.             SelectWindow((WindowRef)theDocument->theWindow);
  131.          else
  132.             SelectWindow((WindowRef)theDocument->theWindow);
  133.         }
  134.     else
  135.         {
  136.         SelectWindow(theDocument->theWindow);
  137.         DoContent(theDocument,theEvent);
  138.         }
  139. }
  140.  
  141.  
  142. // *****************************************************************************
  143. // *
  144. // *    DoMouseDown()
  145. // *
  146. // *    (1)    If the mouseDown is not in a draggable object (not in the document's hiliteRgn) call
  147. // *        SelectWindow to bring the window to the front as usual.
  148. // *
  149. // *    (2)    If the mouseDown is in a draggable object and the mouse is released without
  150. // *        dragging, call SelectWindow when the mouse is released.
  151. // *
  152. // *    (3)    If the mouseDown is in a draggable object and a successful drag occurs, SelectWindow
  153. // *        should only be called if the drop occurred in the same window (the DragText function
  154. // *        calls SelectWindow in this case).
  155. // *
  156. // *****************************************************************************
  157. void DoMouseDown(EventRecord* theEvent)
  158. {    
  159.     short        thePart;
  160.     WindowPtr    theWindow;
  161.     Rect        dragRect;
  162.     Document*    theDocument;
  163.  
  164.     thePart = FindWindow(theEvent->where,&theWindow);
  165.     switch(thePart)
  166.         {
  167.         case inMenuBar:
  168.             AdjustMenus();
  169.             DoMenuCommand(MenuSelect(theEvent->where));
  170.             break;
  171.             
  172.         case inSysWindow:
  173.             SystemClick(theEvent,theWindow);
  174.             break;
  175.             
  176.         case inContent:
  177.             theDocument = IsDocumentWindow(theWindow);
  178.             if (theWindow == (WindowPtr)FrontWindow())
  179.                 DoContent(theDocument,theEvent);
  180.             else
  181.                 DoBackgroundContent(theDocument,theEvent);
  182.             break;
  183.             
  184.         case inDrag:
  185.             if (theWindow != (WindowPtr)FrontWindow())
  186.                 SelectWindow((WindowRef)theWindow);
  187.             dragRect = qd.screenBits.bounds;
  188.             DragWindow((WindowRef)theWindow,theEvent->where,&dragRect);
  189.             break;
  190.             
  191.         case inGrow:
  192.             if (theDocument = IsDocumentWindow(theWindow))
  193.                 {
  194.                 GrowDocumentWindow(theWindow,theEvent->where);
  195.                 if (theDocument->theTE != NULL)
  196.                     TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
  197.                 }
  198.             break;
  199.         
  200.         case inZoomIn:
  201.         case inZoomOut:
  202.             if (theDocument = IsDocumentWindow(theWindow))
  203.                 if ((TrackBox(theWindow,theEvent->where,thePart))&&(theDocument))
  204.                     DoZoomDocument(theDocument,theWindow,thePart);
  205.             break;
  206.  
  207.         case inGoAway:
  208.             if (theDocument = IsDocumentWindow(theWindow))
  209.                 if (TrackGoAway((WindowRef)theWindow,theEvent->where))
  210.                     {
  211.                     CloseDocument(theDocument,false);
  212.                     AdjustMenus();
  213.                     DrawMenuBar();
  214.                     }
  215.             break;
  216.         }
  217. }
  218.  
  219.  
  220. // *****************************************************************************
  221. // *
  222. // *    DoKey()
  223. // *
  224. // *    Called each time a character is typed on the keyboard to
  225. // *    be entered into a document window.
  226. // *
  227. // *****************************************************************************
  228. void DoKey(char theChar)
  229. {    
  230.     WindowPtr    theWindow;
  231.     Document*    theDocument;
  232.  
  233.     if (theWindow = (WindowPtr)FrontWindow())
  234.         {
  235.         if (theDocument = IsDocumentWindow(theWindow))
  236.             {
  237.             SetPort((GrafPtr)theDocument->theWindow);
  238.  
  239.             if (theDocument->theTE != NULL)
  240.                 {
  241.                 TEKey(theChar,theDocument->theTE);
  242.                 AdjustScrollBar(theDocument);
  243.                 theDocument->dirty = true;
  244.                 TEGetHiliteRgn(theDocument->hiliteRgn,theDocument->theTE);
  245.  
  246.                 if ((theChar < 0x1C) || (theChar > 0x1F))
  247.                     DisableUndoDrag();
  248.                 }
  249.             }
  250.         }
  251. }
  252.  
  253.  
  254. // *****************************************************************************
  255. // *
  256. // *    DoKeyDown()
  257. // *
  258. // *****************************************************************************
  259. void DoKeyDown(EventRecord* theEvent)
  260. {    
  261.     char theChar;
  262.  
  263.     theChar = theEvent->message & charCodeMask;
  264.  
  265.     if (theEvent->modifiers & cmdKey)
  266.         {
  267.         AdjustMenus();
  268.         DoMenuCommand(MenuKey(theChar));
  269.         }
  270.     else
  271.         DoKey(theChar);
  272. }
  273.  
  274.  
  275. // *****************************************************************************
  276. // *
  277. // *    DoActivate()
  278. // *
  279. // *****************************************************************************
  280. void DoActivate(EventRecord* theEvent)
  281. {    
  282.     WindowPtr    theWindow;
  283.     Document*    theDocument;
  284.  
  285.     if (theWindow = (WindowPtr)theEvent->message)
  286.         if (theDocument = IsDocumentWindow(theWindow))
  287.             DoActivateDocument(theDocument,(theEvent->modifiers & activeFlag));
  288. }
  289.  
  290.  
  291. // *****************************************************************************
  292. // *
  293. // *    DoUpdate()
  294. // *
  295. // *****************************************************************************
  296. void DoUpdate(EventRecord* theEvent)
  297. {    
  298.     Document* theDocument;
  299.  
  300.     if (theDocument = IsDocumentWindow((WindowPtr)theEvent->message))
  301.         UpdateWindow(theDocument);
  302. }
  303.  
  304.  
  305. // *****************************************************************************
  306. // *
  307. // *    DoOSEvent()
  308. // *
  309. // *****************************************************************************
  310. void DoOSEvent(EventRecord* theEvent)
  311. {    
  312.     Document* theDocument;
  313.  
  314.     switch ((theEvent->message >> 24) & 0x0FF)
  315.         {
  316.         case suspendResumeMessage:
  317.             gInBackground = (theEvent->message & resumeFlag) == 0;
  318.             if (theDocument = IsDocumentWindow((WindowPtr)FrontWindow()))
  319.                 DoActivateDocument(theDocument, !gInBackground);
  320.             break;
  321.         }
  322. }
  323.  
  324.  
  325. // *****************************************************************************
  326. // *
  327. // *    MyHandleOAPP()
  328. // *
  329. // *****************************************************************************
  330. pascal OSErr MyHandleOAPP(AppleEvent* /*theAppleEvent*/, AppleEvent* /*reply*/, long /*handlerRefCon*/)
  331. {
  332.     return noErr;
  333. }
  334.  
  335.  
  336. // *****************************************************************************
  337. // *
  338. // *    MyHandleODOC()
  339. // *
  340. // *****************************************************************************
  341. pascal OSErr MyHandleODOC(AppleEvent* theAppleEvent, AppleEvent* /*reply*/, long /*handlerRefCon*/)
  342. {    
  343.     AEDescList    docList;
  344.     AEKeyword    keyword;
  345.     DescType    returnedType;
  346.     FSSpec        theFSSpec;
  347.     Size        actualSize;
  348.     long        itemsInList;
  349.     short        index;
  350.     OSErr        result = noErr;
  351.     FInfo        fileInfo;
  352.  
  353.     if (result = AEGetParamDesc(theAppleEvent,keyDirectObject,typeAEList,&docList))
  354.         return result;
  355.  
  356.     if (result = AECountItems(&docList,&itemsInList))
  357.         return result;
  358.  
  359.     for (index=1;index<=itemsInList;index++)
  360.         {
  361.         if (result = AEGetNthPtr(&docList,index,typeFSS,&keyword,&returnedType,(Ptr)&theFSSpec,sizeof(FSSpec),&actualSize))
  362.             return result;
  363.  
  364.         // decide if the doc we are opening is a PICT or TEXT
  365.         result = FSpGetFInfo(&theFSSpec,&fileInfo);
  366.         if (result == noErr)
  367.             {
  368.             if (fileInfo.fdType == 'TEXT')
  369.                 DoOpenFile(&theFSSpec,false);
  370.             else
  371.                 DoOpenFile(&theFSSpec,true);
  372.             }
  373.         }
  374.     return result;
  375. }
  376.  
  377.  
  378. // *****************************************************************************
  379. // *
  380. // *    MyHandleQUIT()
  381. // *
  382. // *****************************************************************************
  383. pascal OSErr MyHandleQUIT(AppleEvent* /*theAppleEvent*/, AppleEvent* /*reply*/, long /*handlerRefCon*/)
  384. {    
  385.     Document* theDocument;
  386.  
  387.     gQuitting = true;
  388.     
  389.     while ((gQuitting) && (theDocument = IsDocumentWindow((WindowPtr)FrontWindow())))
  390.         CloseDocument(theDocument,true);
  391.     
  392.     if (gQuitting)
  393.         gQuit = true;
  394.  
  395.     return noErr;
  396. }
  397.  
  398.  
  399. // *****************************************************************************
  400. // *
  401. // *    ScrollProc()
  402. // *
  403. // *****************************************************************************
  404. pascal void ScrollProc(ControlHandle theControl, short theCode)
  405. {    
  406.     short        delta = 0, pageDelta;
  407.     Document*    theDocument;
  408.     Rect        viewRect;
  409.     SInt32         theCtrlRef = GetControlReference(theControl);
  410.  
  411.     if (theCode == 0)
  412.         return;
  413.  
  414.     theDocument = (Document*)theCtrlRef;
  415.     
  416.     viewRect = (**(theDocument->theTE)).viewRect;
  417.     pageDelta = ((viewRect.bottom - viewRect.top) / ScrollResolution) - 1;
  418.  
  419.     switch(theCode)
  420.         {
  421.         case kControlUpButtonPart:
  422.             delta = -1;
  423.             break;
  424.         case kControlDownButtonPart:
  425.             delta = 1;
  426.             break;
  427.         case kControlPageUpPart:
  428.             delta = -pageDelta;
  429.             break;
  430.         case kControlPageDownPart:
  431.             delta = pageDelta;
  432.             break;
  433.         }
  434.  
  435.     SetControlValue(theControl,GetControlValue(theControl) + delta);
  436.     AdjustDocumentView((Document*)theDocument);
  437. }
  438.  
  439.  
  440. // *****************************************************************************
  441. // *
  442. // *    DoHighLevelEvent()
  443. // *
  444. // *****************************************************************************
  445. void DoHighLevelEvent(EventRecord* theEvent)
  446. {
  447.     AEProcessAppleEvent(theEvent);
  448. }
  449.  
  450.  
  451. // *****************************************************************************
  452. // *
  453. // *    DoEvent()
  454. // *
  455. // *****************************************************************************
  456. void DoEvent(EventRecord* theEvent)
  457. {    
  458.     WindowPtr    theWindow;
  459.     Document*    theDocument;
  460.  
  461.     if (theWindow = (WindowPtr)FrontWindow())
  462.         if (theDocument = IsDocumentWindow(theWindow))
  463.             gFrontDocument = theDocument;
  464.  
  465.     switch(theEvent->what)
  466.         {
  467.         case mouseDown:
  468.             DoMouseDown(theEvent);
  469.             break;
  470.         case mouseUp:
  471.             break;
  472.         case keyDown:
  473.         case autoKey:
  474.             DoKeyDown(theEvent);
  475.             break;
  476.         case activateEvt:
  477.             DoActivate(theEvent);
  478.             break;
  479.         case updateEvt:
  480.             DoUpdate(theEvent);
  481.             break;
  482.         case osEvt:
  483.             DoOSEvent(theEvent);
  484.             break;
  485.         case kHighLevelEvent:
  486.             DoHighLevelEvent(theEvent);
  487.             break;
  488.         }
  489. }
  490.  
  491.  
  492. // *****************************************************************************
  493. // *
  494. // *    DoIdle()
  495. // *
  496. // *****************************************************************************
  497. void DoIdle()
  498. {    
  499.     WindowPtr    theWindow;
  500.     Document*    theDocument;
  501.  
  502.     if (theWindow = (WindowPtr)FrontWindow())
  503.         if (theDocument = IsDocumentWindow(theWindow))
  504.             if (theDocument->theTE != NULL)
  505.                 {
  506.                 SetPort((GrafPtr)theDocument->theWindow);
  507.                 TEIdle(theDocument->theTE);
  508.                 }
  509. }
  510.  
  511.  
  512. // *****************************************************************************
  513. // *
  514. // *    EventLoop()
  515. // *
  516. // *****************************************************************************
  517. void EventLoop()
  518. {    
  519.     short        gotEvent;
  520.     EventRecord    theEvent;
  521.     RgnHandle    theMouseRgn;
  522.  
  523.     theMouseRgn = NewRgn();
  524.     do     {
  525.         gotEvent = WaitNextEvent(everyEvent,&theEvent,SleepDuration,theMouseRgn);
  526.         if (gotEvent)
  527.             {
  528.             AdjustCursor(theEvent.where,theMouseRgn);
  529.             DoEvent(&theEvent);
  530.             AdjustCursor(theEvent.where,theMouseRgn);
  531.             }
  532.         else
  533.             DoIdle();
  534.         }
  535.     while (!gQuit);
  536.  
  537.     DisposeRgn(theMouseRgn);
  538. }